package com.bosssoft.goods.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.goods.domain.vo.StBatchGoodUpdateVO;
import com.bosssoft.goods.domain.vo.StBatchGoodOperateVO;
import com.bosssoft.goods.domain.vo.StGoodUpdateVO;
import com.bosssoft.goods.service.ShoppingTrolleyService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品购物车API接口
 * @author Fang_Sir
 */
@RestController
@RequestMapping("/shopping/trolley")
@Api(tags = "6-购物车管理操作")
public class ShoppingTrolleyController {
    @Autowired
    private ShoppingTrolleyService shoppingTrolleyService;

    @GetMapping("/getGoods/{uid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户编号", required = true, dataType = "long", paramType = "path")
    })
    @ApiOperation(value = "通过用户编号查询购物车")
    public Result<?> getUerShoppingTrolley(@PathVariable("uid") long uid){
        return Result.ok(shoppingTrolleyService.getShoppingTrolleyWithGoodInfo(uid));
    }

    @PostMapping("/getGoodsBatch")
    @ApiOperation(value = "批量获取用户购物车商品")
    public Result<?> getGoodInfosWithGoodIds(@Valid @RequestBody StBatchGoodOperateVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        return Result.ok(shoppingTrolleyService.getBatchGoodsInfo(info));
    }

    /**
     * 添加商品
     */
    @PostMapping("/addGoodBatch")
    @ApiOperation(value = "批量添加商品到购物车(返回添加失败的商品编号)")
    @ApiResponses({
            @ApiResponse(code = 200,message = "添加失败的商品编号数组")
    })
    public Result<?> addGoodBatch(@Valid @RequestBody StBatchGoodUpdateVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        return Result.ok(shoppingTrolleyService.addGood(info.getGoods()));
    }

    /**
     * 添加商品
     */
    @PostMapping("/addGood")
    @ApiOperation(value = "添加单个商品到购物车")
    public Result<?> addGood(@Valid @RequestBody StGoodUpdateVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        return shoppingTrolleyService.addGood(info) ? Result.ok() : Result.error();
    }

    /**
     * 更改购物车购买数量
     * @return
     */
    @PutMapping("/editQty")
    @ApiOperation(value = "更改购物车商品购买数量")
    public Result<?> increaseQty(@Valid @RequestBody StGoodUpdateVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            if(info.getQty() < 1) {
                Result.error("商品购买数量更改失败！请确保购买数量大于0");
            }
            return shoppingTrolleyService.updateGoodQty(info) ? Result.ok() : Result.error("商品购买数量更改失败！");
        } catch (Exception e) {
            return Result.error("请检查是否已将商品添加进购物车！");
        }
    }

    /**
     * 添加商品
     */
    @DeleteMapping("/removeGood")
    @ApiOperation(value = "删除购物车商品")
    public Result<?> removeGood(@Valid @RequestBody StBatchGoodOperateVO info, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        return shoppingTrolleyService.removeGood(info) ? Result.ok() : Result.error();
    }
}
